package com.qx.ddx.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.qx.ddx.DDXApplication;
import com.qx.ddx.R;
import com.qx.ddx.util.SpInfo;
import com.qx.ddx.util.Toast;

import java.io.File;

public class MainActivity extends BaseActivity {

    private TextView table_label_name_txt, table_label_num_txt, welcome_txt;
    private Button order_btn, call_btn, msg_btn, set_btn;
    private View table_label_layout;
    private long mExitTime;
    private String path = Environment.getExternalStorageDirectory().getPath()
            + File.separator + "DDX" + File.separator;
    private String commodityImage = Environment.getExternalStorageDirectory().getPath()
            + File.separator + "DDX" + File.separator + "commodityImage" + File.separator;
    private String image = Environment.getExternalStorageDirectory().getPath()
            + File.separator + "DDX" + File.separator + "image" + File.separator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DDXApplication.getInstance().addActivity(this);
        table_label_name_txt = (TextView) findViewById(R.id.main_table_name);
        table_label_num_txt = (TextView) findViewById(R.id.main_table_num);
        table_label_layout = findViewById(R.id.main_table_label_Layout);
        welcome_txt = (TextView) findViewById(R.id.main_welcome_txt);
        order_btn = (Button) findViewById(R.id.main_order_btn);
        call_btn = (Button) findViewById(R.id.main_call_btn);
        msg_btn = (Button) findViewById(R.id.main_msg_btn);
        set_btn = (Button) findViewById(R.id.main_set_btn);
        File df = new File(path.trim());
        if (!df.exists()) {
            if (df.mkdirs()) {
                File f = new File(commodityImage.trim());
                if (!f.exists()) {
                    if (!f.mkdirs()) {
                        Log.i("File", "commodityImage文件创建不成功！");
                    }
                }
                File af = new File(image.trim());
                if (!af.exists()) {
                    if (!af.mkdirs()) {
                        Log.i("File", "image文件创建不成功！");
                    }
                }
            } else {
                Log.i("File", "path文件创建不成功！");
            }
        } else {
            File f = new File(commodityImage.trim());
            if (!f.exists()) {
                if (!f.mkdirs()) {
                    Log.i("File", "commodityImage文件创建不成功！");
                }
            }
            File af = new File(image.trim());
            if (!af.exists()) {
                if (!af.mkdirs()) {
                    Log.i("File", "image文件创建不成功！");
                }
            }
        }
        setBtnonClikerLintener();
    }

    protected void onResume() {
        super.onResume();
        SharedPreferences sp = getSharedPreferences(SpInfo.Setting, Activity.MODE_PRIVATE);
        String table_num_str = sp.getString(SpInfo.Table_Number, getResources().getString(R.string.tip_set));
        table_label_num_txt.setText(table_num_str);
        String table_name_str = sp.getString(SpInfo.Table_Name, "");
        table_label_name_txt.setText(table_name_str);
        String num_str = table_label_num_txt.getText().toString();
        if (num_str.equals(getResources().getString(R.string.tip_set))) {
            table_label_layout.setFocusable(true);
            table_label_layout.setClickable(true);
            table_label_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SetActivity.class);
                    MainActivity.this.startActivity(intent);
                }
            });
        } else {
            table_label_layout.setFocusable(false);
            table_label_layout.setClickable(false);
        }
    }

    private void setBtnonClikerLintener() {
        MainOnClickListener mainOnClickListener = new MainOnClickListener();
        order_btn.setOnClickListener(mainOnClickListener);
        call_btn.setOnClickListener(mainOnClickListener);
        msg_btn.setOnClickListener(mainOnClickListener);
        set_btn.setOnClickListener(mainOnClickListener);
    }


    private class MainOnClickListener implements View.OnClickListener {
        Intent intent = null;

        public void onClick(View v) {
            if (intent == null) {
                intent = new Intent();
            }
            String ipStr = getIPStr();
            switch (v.getId()) {
                case R.id.main_order_btn:
//                    if (ipStr.equals("")) {
//                        Toast.show(R.mipmap.tips_warning, "请配置服务器地址");
//                        return;
//                    }
                    intent.setClass(MainActivity.this, OrderActivity.class);
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.main_call_btn:
//                    if (ipStr.equals("")) {
//                        Toast.show(R.mipmap.tips_warning, "请配置服务器地址");
//                        return;
//                    }
                    intent.setClass(MainActivity.this, CallActivity.class);
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.main_msg_btn:
//                    if (ipStr.equals("")) {
//                        Toast.show(R.mipmap.tips_warning, "请配置服务器地址");
//                        return;
//                    }
                    intent.setClass(MainActivity.this, MsgActivity.class);
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.main_set_btn:
                    intent.setClass(MainActivity.this, SetActivity.class);
                    MainActivity.this.startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    }

    public String getIPStr() {
        return ((DDXApplication) getApplication()).getIPAndPort();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.show(R.mipmap.tips_smile, getResources().getString(R.string.tip_quit));
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
